﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using System.Threading;
using System.Diagnostics;
using System.IO;
using AutoUpdate.Common; 
namespace AutoUpdate
{
    public partial class TienTrinh : Telerik.WinControls.UI.RadForm
    {
        public static int trangthai = 0;
        public int trangthaicopyexe, trangthaicopydll, trangthaicopyreport;
        public TienTrinh(int trangthaicopyexetv,int trangthaicopydlltv,int trangthaicopyreporttv)
        {
            InitializeComponent();
            trangthaicopyexe = trangthaicopyexetv;
            trangthaicopydll = trangthaicopydlltv;
            trangthaicopyreport = trangthaicopyreporttv;
            backgroundWorker1 = new BackgroundWorker();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;

            backgroundWorker1.DoWork += backgroundWorker1_DoWork;
            backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
            backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
        }
        public void chayexe(string exepatch)
        {

            //Application.StartupPath + @"\AutoUpdate.EXE";
            try
            {
                ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = exepatch };

                Process.Start(startInfo);

            }
            catch (Exception ex)
            {
                RadMessageBox.Show("Lỗi " + ex.ToString(), "Cảnh Báo", MessageBoxButtons.OK, RadMessageIcon.Error);
            }
        }
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (!backgroundWorker1.CancellationPending)
            {
                //     radLabel1.Text = "Hoàn Thành Được " + e.ProgressPercentage + "% Tiến Trình Update Chương Trình Từ Version ";
                radLabel1.Text = String.Format("Hoàn Thành Được {0}% Tiến Trình Update Chương Trình ", e.ProgressPercentage);
                //  lblPercent.Text = e.ProgressPercentage + "%";
                //  radProgressBar1.PerformStep();

            }


        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            RadMessageBox.Show("Tiến trình cập nhật thành công !!! ", "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Info);
            this.Close();

            chayexe(Application.StartupPath + @"\Bitis_KeToanThue_Pro_Update.EXE");


        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            DoSomething();
        }
        private void DoSomething()
        {
            int lancopy = 0;


            /* try
             {
                 ch.CopyFolder(frmLogin.duongdancauhinh, Application.StartupPath);
                 ketqua = 1;

             }
             catch(Exception ex)
             {
                 ketqua = 0;
                 RadMessageBox.Show("Lỗi :"+ex.ToString(), "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Error);
             }*/
            while (radProgressBar1.Value1 < radProgressBar1.Maximum)
            {
                if (backgroundWorker1.CancellationPending)
                {

                    break;


                }
                //báo cáo tiến độ của tiến trình
                if (lancopy == 0)
                {
                    try
                    {
                         string DuongDanIniFileClient = "", DuongDanUpdateDLLClident = "", DuongDanUpdateEXEClident = "", DuongDanUpdateREPORTClident = "", VersionUpdateDLLClident = "",
                VersionUpdateEXEClident = "", VersionUpdateREPORTClident = "", DuongDanUpdateDLLServer = "", DuongDanUpdateEXEServer = "", DuongDanUpdateREPORTServer = "",
                DuongDanIniFileServer = "", VersionUpdateDLLServer = "", VersionUpdateEXEServer = "", VersionUpdateREPORTServer = "";

            WriteReadIniFile iniclient = new WriteReadIniFile(Application.StartupPath+@"\update.ini");
            try
            {
                DuongDanIniFileClient = iniclient.IniReadValue("DuongDan", "DuongDanIniFileServer");
                DuongDanUpdateDLLClident = iniclient.IniReadValue("DuongDan", "DuongDanUpdateDLL");
                DuongDanUpdateEXEClident = iniclient.IniReadValue("DuongDan", "DuongDanUpdateEXE");
                DuongDanUpdateREPORTClident = iniclient.IniReadValue("DuongDan", "DuongDanUpdateREPORT");
                VersionUpdateDLLClident = iniclient.IniReadValue("Version", "VersionUpdateDLL");
                VersionUpdateEXEClident = iniclient.IniReadValue("Version", "VersionUpdateEXE");
                VersionUpdateREPORTClident = iniclient.IniReadValue("Version", "VersionUpdateREPORT");
            }
            catch
            {
                DuongDanIniFileClient = string.Empty;
                DuongDanUpdateDLLClident = string.Empty;
                DuongDanUpdateEXEClident = string.Empty;
                DuongDanUpdateREPORTClident = string.Empty;
                VersionUpdateDLLClident = string.Empty;
                VersionUpdateEXEClident = string.Empty;
                VersionUpdateREPORTClident = string.Empty;
            }
            if (File.Exists(String.Format(DuongDanIniFileClient)))
            {
                WriteReadIniFile iniServer = new WriteReadIniFile(DuongDanIniFileClient);
                try
                {

                    DuongDanIniFileServer = iniServer.IniReadValue("DuongDan", "DuongDanIniFileServer");
                    DuongDanUpdateDLLServer = iniServer.IniReadValue("DuongDan", "DuongDanUpdateDLL");
                    DuongDanUpdateEXEServer = iniServer.IniReadValue("DuongDan", "DuongDanUpdateEXE");
                    DuongDanUpdateREPORTServer = iniServer.IniReadValue("DuongDan", "DuongDanUpdateREPORT");
                    VersionUpdateDLLServer = iniServer.IniReadValue("Version", "VersionUpdateDLL");
                    VersionUpdateEXEServer = iniServer.IniReadValue("Version", "VersionUpdateEXE");
                    VersionUpdateREPORTServer = iniServer.IniReadValue("Version", "VersionUpdateREPORT");
                }
                catch
                {
                    DuongDanIniFileServer = string.Empty;
                    DuongDanUpdateDLLServer = string.Empty;
                    DuongDanUpdateEXEServer = string.Empty;
                    DuongDanUpdateREPORTServer = string.Empty;
                    VersionUpdateDLLServer = string.Empty;
                    VersionUpdateEXEServer = string.Empty;
                    VersionUpdateREPORTServer = string.Empty;
                }
            }
            else
            {
                DuongDanIniFileServer = string.Empty;
                DuongDanUpdateDLLServer = string.Empty;
                DuongDanUpdateEXEServer = string.Empty;
                DuongDanUpdateREPORTServer = string.Empty;
                VersionUpdateDLLServer = string.Empty;
                VersionUpdateEXEServer = string.Empty;
                VersionUpdateREPORTServer = string.Empty;
            }
            if (trangthaicopydll == 1)
            {
                CopyFolder(DuongDanUpdateDLLServer, Application.StartupPath);
            }
            if (trangthaicopyexe == 1)
            {
                CopyFolder(DuongDanUpdateEXEServer, Application.StartupPath);
            }
            if (trangthaicopyreport == 1)
            {
                CopyFolder(DuongDanUpdateREPORTServer, Application.StartupPath+@"\RPT");
            }
            iniclient.IniWriteValue("DuongDan","DuongDanIniFileServer",DuongDanIniFileServer);
            iniclient.IniWriteValue("DuongDan", "DuongDanUpdateDLL",DuongDanUpdateDLLServer);
             iniclient.IniWriteValue("DuongDan", "DuongDanUpdateEXE",DuongDanUpdateEXEServer);
            iniclient.IniWriteValue("DuongDan", "DuongDanUpdateREPORT",DuongDanUpdateREPORTServer);
             iniclient.IniWriteValue("Version", "VersionUpdateDLL",VersionUpdateDLLServer);
           iniclient.IniWriteValue("Version", "VersionUpdateEXE",VersionUpdateEXEServer);
           iniclient.IniWriteValue("Version", "VersionUpdateREPORT",VersionUpdateREPORTServer);
                        lancopy = 1;

                    }
                    catch
                    {
                        lancopy = 0;
                    }
                }

                radProgressBar1.Value1 = radProgressBar1.Value1 + 10;
                backgroundWorker1.ReportProgress(radProgressBar1.Value1);
                Thread.Sleep(1000);

            }


            // Thread.Sleep(20000);
        }



        public void CopyFolder(string SourceFolder, string DestFolder)
        {

            if (Directory.Exists(SourceFolder)) // folder ton tai thi moi thuc hien copy
            {
                Directory.CreateDirectory(DestFolder); //Tao folder moi
                string[] files = Directory.GetFiles(SourceFolder);
                //Neu co file thy phai copy file
                foreach (string file in files)
                {
                    string name = Path.GetFileName(file);
                    string dest = Path.Combine(DestFolder, name);
                    //string df = CalculateFileSize(file.Length);

                    File.Copy(file, dest, true);


                }

                string[] folders = Directory.GetDirectories(SourceFolder);
                foreach (string folder in folders)
                {
                    string name = Path.GetFileName(folder);
                    string dest = Path.Combine(DestFolder, name);
                    CopyFolder(folder, dest);
                }
            }
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            DialogResult kq = RadMessageBox.Show("Bạn có chắc hủy tiến trình cập nhật không?", "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
            if (kq == DialogResult.Yes)
            {
                // backgroundWorker1.CancellationPending = true;
                //   radProgressBar1.Value1 = radProgressBar1.Maximum;
                trangthai = 1;
                RadMessageBox.Show("Tiến trình cập nhật đã được hủy !!! ", "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                backgroundWorker1.CancelAsync();
                backgroundWorker1.Dispose();
                this.Close();
                //   dongform();
                chayexe(Application.StartupPath + @"\Bitis_KeToanThue_Pro_Update.EXE");

            }
            else
            {
                trangthai = 0;
            }
        }

        private void TienTrinh_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void TienTrinh_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (backgroundWorker1.IsBusy && trangthai == 0)
            {
                DialogResult kq = RadMessageBox.Show("Bạn có chắc hủy tiến trình cập nhật không?", "Thông Báo", MessageBoxButtons.YesNo, RadMessageIcon.Question);
                if (kq == DialogResult.Yes)
                {

                    RadMessageBox.Show("Tiến trình cập nhật đã được hủy !!! ", "Thông Báo", MessageBoxButtons.OK, RadMessageIcon.Exclamation);
                    backgroundWorker1.CancelAsync();
                    backgroundWorker1.Dispose();

                    chayexe(Application.StartupPath + @"\Bitis_KeToanThue_Pro_Update.EXE");


                }
            }
        }
    }
}
